import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class EditAccountUI extends JPanel {
	private JLabel txtUsername;
	private JTextField txtFName;
	private JTextField txtLName;
	private JPasswordField txtPassword;
	private JPasswordField txtRetypePassword;
	LoginUI login;
	String globalusername = login.GlobalUsername.getText().trim();
	String oldfname;
	String oldlname;
	int checker;

	/**
	 * Create the panel.
	 */
	public EditAccountUI() {
		setLayout(null);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(56, 43, 63, 14);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(56, 128, 63, 14);
		add(lblPassword);
		
		JLabel lblFName = new JLabel("First Name:");
		lblFName.setBounds(353, 49, 89, 14);
		add(lblFName);
		
		JLabel lblLName = new JLabel("Last Name:");
		lblLName.setBounds(353, 128, 89, 14);
		add(lblLName);
		
		txtUsername = new JLabel();
		txtUsername.setBounds(56, 68, 165, 30);
		add(txtUsername);
		
		txtFName = new JTextField();
		txtFName.setBounds(353, 68, 165, 30);
		add(txtFName);
		txtFName.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnSubmitActionPerformed(arg0);
			}
		});
		btnSubmit.setBounds(175, 320, 89, 23);
		add(btnSubmit);
		
		txtLName = new JTextField();
		txtLName.setColumns(10);
		txtLName.setBounds(353, 153, 165, 30);
		add(txtLName);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(56, 153, 165, 30);
		add(txtPassword);
		
		txtRetypePassword = new JPasswordField();
		txtRetypePassword.setBounds(56, 238, 165, 30);
		add(txtRetypePassword);
		
		JLabel lblRetypePassword = new JLabel("Re-type Password:");
		lblRetypePassword.setBounds(56, 213, 124, 14);
		add(lblRetypePassword);
		
		JButton btnCancel = new JButton("Reset form");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnCancelActionPerformed(e);
			}
		});
		btnCancel.setBounds(338, 320, 89, 23);
		add(btnCancel);
		
		loadDetails();
	}
	
	private void loadDetails(){
		Connection con = DBConnector.getConnection();
		String query = "SELECT username, lname, fname, usertype FROM useraccounts WHERE username = '"+globalusername+"'";
		ResultSet rs= DBConnector.executeQuery(con, query);

		String DBusername = null;
		String DBfname = null;
		String DBlname = null;
		String DBusertype = null;
		
		txtPassword.setText("");
		txtRetypePassword.setText("");
		
		try {
			while(rs.next()){
				DBusername = rs.getString("username");
				DBfname = rs.getString("fname");
				DBlname = rs.getString("lname");
				DBusertype = rs.getString("usertype");
			}
			
			txtUsername.setText(DBusername);
			txtFName.setText(DBfname);
			txtLName.setText(DBlname);
			
			oldfname = DBfname;
			oldlname = DBlname;
			
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void btnSubmitActionPerformed(ActionEvent arg0) {
		try{
			if (oldfname.compareTo(txtFName.getText()) != 0 || oldlname.compareTo(txtLName.getText()) != 0 || (!txtPassword.getText().isEmpty() && !txtRetypePassword.getText().isEmpty()))
			if (JOptionPane.showConfirmDialog(null, "Are you sure with all your modifications?", "WARNING",
		        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			if (oldfname.compareTo(txtFName.getText()) != 0) {
				String query = "UPDATE useraccounts SET FName = '"+txtFName.getText()+"' WHERE username = '"+globalusername+"'";
				
				Connection con = DBConnector.getConnection();
				
				try {
					DBConnector.executeUpdate(con, query);
					JOptionPane.showMessageDialog(null, "First Name has been updated.");
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (oldlname.compareTo(txtLName.getText()) != 0) {
				String query = "UPDATE useraccounts SET LName = '"+txtLName.getText()+"' WHERE username = '"+globalusername+"'";
				
				Connection con = DBConnector.getConnection();
				
				try {
					DBConnector.executeUpdate(con, query);
					JOptionPane.showMessageDialog(null, "Last Name has been updated.");
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(!txtPassword.getText().isEmpty() && !txtRetypePassword.getText().isEmpty()){
				if(txtPassword.getText().compareTo(txtRetypePassword.getText()) == 0){
					String query = "UPDATE useraccounts SET password = '"+txtPassword.getText()+"' WHERE username = '"+globalusername+"'";
					
					Connection con = DBConnector.getConnection();
					
					try {
						DBConnector.executeUpdate(con, query);
						JOptionPane.showMessageDialog(null, "Password has been updated.");
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
					DBConnector.alertUser("Passwords doesn't match.");
			}
			}
			loadDetails();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void btnCancelActionPerformed(ActionEvent arg0) {
		loadDetails();
	}
	
	private boolean checkUsername(){
		String query = "SELECT COUNT(*) AS username FROM useraccounts WHERE username = '"+txtUsername.getText()+"'";
		
		Connection con = DBConnector.getConnection();
		ResultSet rs = DBConnector.executeQuery(con, query);

		try {
			while(rs.next())
			    checker = rs.getInt("username");
		
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		if(checker == 1)	// Username existing
			return false;
		else				// Username not existing and ready to use
			return true;
	}

}
